package cn.bestick.action;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import cn.bestick.dao.DataDicDao;
import cn.bestick.dao.DataDicTypeDao;
import cn.bestick.model.DataDic;
import cn.bestick.model.DataDicType;
import cn.bestick.model.PageBean;
import cn.bestick.util.DbUtil;
import cn.bestick.util.NavUtil;
import cn.bestick.util.PageUtil;
import cn.bestick.util.PropertiesUtil;
import cn.bestick.util.ResponseUtil;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Bestick
 *
 *         be stick...
 *
 *         2015年9月5日 下午1:16:08
 */
public class DataDicAction extends ActionSupport implements ServletRequestAware {

	private static final long serialVersionUID = 1L;

	private HttpServletRequest request;

	private DataDicDao dataDicDao = new DataDicDao();

	private DataDicTypeDao dataDicTypeDao = new DataDicTypeDao();

	private List<DataDic> dataDicList = new ArrayList<DataDic>();

	private List<DataDicType> dataDicTypeList = new ArrayList<DataDicType>();

	private String mainPage;

	private String navCode;

	private String s_ddTypeName;

	private Integer page;

	private int total;

	private String pageCode;

	private Integer ddId;

	private Integer pageSize;

	private DataDic dataDic;

	public List<DataDicType> getDataDicTypeList() {
		return dataDicTypeList;
	}

	public void setDataDicTypeList(List<DataDicType> dataDicTypeList) {
		this.dataDicTypeList = dataDicTypeList;
	}

	public DataDic getDataDic() {
		return dataDic;
	}

	public void setDataDic(DataDic dataDic) {
		this.dataDic = dataDic;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getDdId() {
		return ddId;
	}

	public void setDdId(Integer ddId) {
		this.ddId = ddId;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public String getPageCode() {
		return pageCode;
	}

	public void setPageCode(String pageCode) {
		this.pageCode = pageCode;
	}

	public List<DataDic> getDataDicList() {
		return dataDicList;
	}

	public void setDataDicList(List<DataDic> dataDicList) {
		this.dataDicList = dataDicList;
	}

	public String getMainPage() {
		return mainPage;
	}

	public void setMainPage(String mainPage) {
		this.mainPage = mainPage;
	}

	public String getNavCode() {
		return navCode;
	}

	public void setNavCode(String navCode) {
		this.navCode = navCode;
	}

	public String getS_ddTypeName() {
		return s_ddTypeName;
	}

	public void setS_ddTypeName(String s_ddTypeName) {
		this.s_ddTypeName = s_ddTypeName;
	}

	public String list() {
		if (page == null) {
			page = 1;
		}
		Connection con = null;
		try {
			DataDic s_dataDic = new DataDic();
			HttpSession session = request.getSession();
			if (s_ddTypeName != null) {
				s_dataDic.setDdTypeName(s_ddTypeName);
				session.setAttribute("s_ddTypeName", s_ddTypeName);
			} else {
				Object o = session.getAttribute("s_ddTypeName");
				if (o != null) {
					s_ddTypeName = (String) o;
					s_dataDic.setDdTypeName(s_ddTypeName);
				}
			}
			pageSize = Integer.parseInt(PropertiesUtil.getValue("pageSize"));
			PageBean pageBean = new PageBean(page, pageSize);
			con = DbUtil.getInstance().getCon();
			dataDicList = dataDicDao.dataDicList(con, s_dataDic, pageBean);
			total = dataDicDao.dataDicCount(con, s_dataDic);
			pageCode = PageUtil.genPagation(request.getContextPath()
					+ "/dataDic!list", total, page, pageSize);
			navCode = NavUtil.getNavgation("系统管理", "数据字典维护");
			mainPage = "dataDic/dataDicList.jsp";
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				DbUtil.getInstance().closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return SUCCESS;
	}

	public String delete() {
		Connection con = null;
		try {
			con = DbUtil.getInstance().getCon();
			JSONObject result = new JSONObject();
			result.put("success", "删除成功！");
			dataDicDao.dataDicDelete(con, ddId);
			ResponseUtil.write(result, ServletActionContext.getResponse());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				DbUtil.getInstance().closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String preSave() {
		Connection con = null;
		try {
			con = DbUtil.getInstance().getCon();
			dataDicTypeList = dataDicTypeDao.getDataDicTypeList(con);
			if (ddId != null) {
				dataDic = dataDicDao.getDataDicById(con, ddId);
				navCode = NavUtil.getNavgation("系统管理", "数据字典修改");
			} else {
				navCode = NavUtil.getNavgation("系统管理", "数据字典添加");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				DbUtil.getInstance().closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		mainPage = "dataDic/dataDicSave.jsp";
		return SUCCESS;
	}

	public String save() {
		Connection con = null;
		try {
			con = DbUtil.getInstance().getCon();
			if (ddId != null) {
				dataDic.setDdId(ddId);
				dataDicDao.dataDicUpdate(con, dataDic);
			}else {
				dataDicDao.dataDicAdd(con, dataDic);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				DbUtil.getInstance().closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "save";
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
}
